﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDevTools.ViewModels
{
    internal class TimestampControlViewModel : ObservableObject
    {
        private DateTime now = DateTime.Now;

        /// <summary>
        /// 当前北京时间
        /// </summary>
        public DateTime Now
        {
            get
            {
                return this.now;
            }

            set
            {
                this.SetProperty(ref this.now, value);
            }
        }

        private long nowTimestamp = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds();

        /// <summary>
        /// 当前北京时间时间戳
        /// </summary>
        public long NowTimestamp
        {
            get
            {
                return this.nowTimestamp;
            }

            set
            {
                this.SetProperty(ref this.nowTimestamp, value);
            }
        }

        private int localTimeUnit;

        /// <summary>
        /// 北京时间单位
        /// </summary>
        public int LocalTimeUnit
        {
            get
            {
                return this.localTimeUnit;
            }

            set
            {
                this.SetProperty(ref this.localTimeUnit, value);
            }
        }

        private DateTime utcNow = DateTime.UtcNow;

        /// <summary>
        /// 当前utc时间
        /// </summary>
        public DateTime UTCNow
        {
            get
            {
                return this.utcNow;
            }

            set
            {
                this.SetProperty(ref this.utcNow, value);
            }
        }

        private long utcNowTimestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

        /// <summary>
        /// 当前utc时间时间戳
        /// </summary>
        public long UTCNowTimestamp
        {
            get
            {
                return this.utcNowTimestamp;
            }

            set
            {
                this.SetProperty(ref this.utcNowTimestamp, value);
            }
        }

        private int utcTimeUnit;

        /// <summary>
        /// Utc时间单位
        /// </summary>
        public int UtcTimeUnit
        {
            get
            {
                return this.utcTimeUnit;
            }

            set
            {
                this.SetProperty(ref this.utcTimeUnit, value);
            }
        }


        public RelayCommand LocalTimeToTimestampCmd
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.NowTimestamp = this.localTimeUnit == 0 ? new DateTimeOffset(this.now).ToUnixTimeSeconds() : new DateTimeOffset(this.now).ToUnixTimeMilliseconds();
                });
            }
        }

        public RelayCommand TimestampToLocalTimeCmd
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.Now = this.localTimeUnit == 0 ? DateTimeOffset.FromUnixTimeSeconds(this.nowTimestamp).LocalDateTime : DateTimeOffset.FromUnixTimeMilliseconds(this.nowTimestamp).LocalDateTime;
                });
            }
        }

        public RelayCommand UtcTimeToTimestampCmd
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.UTCNowTimestamp = this.utcTimeUnit == 0 ? new DateTimeOffset(this.utcNow).ToUnixTimeSeconds() : new DateTimeOffset(this.utcNow).ToUnixTimeMilliseconds();
                });
            }
        }

        public RelayCommand TimestampToUtcTimeCmd
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.UTCNow = this.utcTimeUnit == 0 ? DateTimeOffset.FromUnixTimeSeconds(this.utcNowTimestamp).DateTime : DateTimeOffset.FromUnixTimeMilliseconds(this.utcNowTimestamp).DateTime;
                });
            }
        }
    }
}
